// pages/goos_detail/index.js
import {request} from "../../request/index.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 获取商品id
    goods_id:"",
    // 获取商品详情信息
    goods_detail:{},
    // 商品是否被收藏
    isCollect:false,
    // 商品全部信息
    goodsinfo:{}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow:function (){
    let pages = getCurrentPages();
    let currentPage = pages[pages.length -1];
    let goods_id = currentPage.options.goods_id;
    this.setData({
      goods_id
    })
    this.getGooddetail();
    
    let collect = wx.getStorageSync("collect") || [];
    console.log(collect)
    console.log(this.data.goods_id)
    let isCollect = collect.some(v=>v.goods_id == this.data.goods_id);
    console.log(isCollect)
    this.setData({
      isCollect
    })
  },
  // onLoad: function (options) {
  //   console.log(options)
  //   this.setData({
  //     goods_id:options.goods_id
  //   });
  //   this.getGooddetail()
  // },
  getGooddetail(){
    request({url: '/goods/detail?goods_id='+this.data.goods_id}).then(result=>{
      // console.log(result);
      this.setData({
        goodsinfo:result.data.message,
        goods_detail:{
         goods_name:result.data.message.goods_name,
         goods_price:result.data.message.goods_price,
         pics:result.data.message.pics,
         goods_introduce:result.data.message.goods_introduce,
         goods_id:result.data.message.goods_id,
         goods_small_logo:result.data.message.goods_small_logo
        }
      });
      // console.log(this.data.goods_detail)
    })
  },
  // 点击图片放大浏览
  fangda(e){
    // console.log(e.currentTarget.dataset.imgurl)
    let c = [];
    for(var i in this.data.goods_detail.pics){
      c[i]= this.data.goods_detail.pics[i].pics_mid
    }
    // console.log(c)
    wx.previewImage({
      current:e.currentTarget.dataset.imgurl, // 当前显示图片的http链接
      urls: c// 需要预览的图片http链接列表
    })
  },
  // 加入购物车
  shop(){
    // 1.获取缓存商品数据
    // 2.判断加入商品中有无存在于购物车中
    // 3.有则让商品加1，无则让商品加入到购物车中且更新缓存信息
    // 4.添加弹窗提示
    let sp = wx.getStorageSync('cart')|| [];
    const index = sp.findIndex(v=>v.goods_id==this.data.goods_detail.goods_id);
    if(index == -1){
      this.data.goods_detail.num = 1;
      this.data.goods_detail.checked =true;
      sp.push(this.data.goods_detail);
    }else{
      sp[index].num++;
    }
    console.log(sp)
    wx.setStorageSync('cart', sp);
    wx.showToast({
      title: '加入成功',
      icon: 'success',
      duration: 2000
    })
  },
  shoucang(){ 
    let collect = wx.getStorageSync("collect") || [];
    let index = collect.findIndex(v=>v.goods_id == this.data.goodsinfo.goods_id);
    let isCollect = false;
    if(index!==-1){
      collect.splice(index,1);
      isCollect = false;
    }else{
      this.data.goodsinfo.isCollect = true;
      collect.push(this.data.goodsinfo);
      console.log(this.data.goodsinfo)
      isCollect = true;
    }
    this.setData({
      isCollect
    });
    wx.setStorageSync('collect', collect);
    console.log(wx.getStorageSync('collect'))
  }
})